/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package remind;

import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
import java.util.*;
/**
 *
 * @author Yevheniy Semenov
 */
public class ImpOut extends HTMLEditorKit.ParserCallback {
    private StringBuffer text;
    private Reader in;
    private final String dbPath = "";
    private final String dbFile = "db";
    private final String dbExt = "txt";
    private final String separator = "†☺†";
    private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    public ImpOut()
    {
        text = new StringBuffer();
    }
    
    public void exportNotes(List<Note> notes) throws IOException
    { 
        String database = "";
        for (int i = 0; i<remind.noteList.size(); i++)
            {
                database += notes.get(i).getName()+this.separator;                
                database += dateFormat.format(notes.get(i).getDate())+this.separator;
                database += notes.get(i).getPriority()+this.separator;
                database += notes.get(i).getActuality()+this.separator;
                database += notes.get(i).getID()+this.separator;
                database += notes.get(i).getText()+"\r\n";
                //this.htmlToText(notes.get(i).getText())+"\r\n";//don't need tag parser
                
            }
            //FileWriter fw = new FileWriter(this.dbPath+this.dbFile+"."+this.dbExt);
            FileWriter fw = new FileWriter("c:\\test.txt");//поки пишу в тестовий текстовік
            fw.write(database);
            fw.close();      
    }
    
    public LinkedList<Note> getNotes() throws FileNotFoundException, IOException, ParseException
    {
        LinkedList<Note> notes = new LinkedList<Note>();
        String fileName = "c:\\test.txt";
        if (new File(fileName).exists())
        {
            FileReader fr = new FileReader(fileName);
            BufferedReader inStream = new BufferedReader(fr);
            String s;
            while ((s = inStream.readLine()) != null)
            {
                String[] lines = s.split(this.separator);
                if (lines.length == 6)
                {
                    Note note = new Note();
                    note.setName(lines[0]);
                    note.setDate(note.strToDate(lines[1]));
                    note.setPriority(Integer.parseInt(lines[2]));
                    if(lines[3]=="false"){note.setActuality(false);
                    }else{note.setActuality(true);
                    }
                     note.setID(Integer.parseInt(lines[4]));
                    note.setText(lines[5]);
                    notes.add(note);
                }
            }
        }
        return notes;
    }
    
    public String htmlToText(String noteText) throws IOException
    {
        in = new StringReader(noteText);
        ParserDelegator delegator = new ParserDelegator();
            delegator.parse(in, this, Boolean.TRUE);
        return this.getText();   
    }
    
    public void handleText(char[] text2, int pos) {
        text.append(text2);
    }
    
    private String getText() {
        return text.toString();
    }
    
}
